<template>
  <div class="article-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑 -->
        <el-breadcrumb class="bread" separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ name: 'home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 数据筛选表单 -->
      <el-form   label-width="80px">
        <el-form-item label="状态">
          <el-radio-group v-model="queryInfo.status">
            <el-radio :label="null" >全部</el-radio>
            <el-radio label="0">草稿</el-radio>
            <el-radio label="1">待审核</el-radio>
            <el-radio label="2">审核通过</el-radio>
            <el-radio label="3">审核失败</el-radio>
            <el-radio label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select
            size="small"
            v-model="queryInfo.channel_id"
            placeholder="请选择"
            clearable
            @clear="clearSelect"
          >
            <el-option v-for="(item,index) in channels" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="日期">
          <el-date-picker
            size="small"
            v-model="value1"
            value-format=“yyyy-MM-dd”
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="searchArticle"
            >筛选</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据列表 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="color:#E6A23C">根据筛选条件共查询到{{totalCount}}条结果</span>
      </div>
      <!-- 数据表格 -->
      <el-table :data="tableData" border style="width: 100%" v-loading="isLoading">
        <el-table-column align="center" prop="date" label="封面" width="180">
            <template slot-scope="scope">
                 <el-image
                  v-if="scope.row.cover.images.length !== 0"
                  lazy
                  style="width: 100px; height: 100px"
                 :src="scope.row.cover.images[0]">
                 </el-image>
                 <el-image v-else>
                    <div slot="error" class="image-slot">
                        <i class="el-icon-picture-outline">暂无图片</i>
                    </div>
                </el-image>
            </template>
        </el-table-column>
        <el-table-column align="center" prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column align="center" prop="address" label="状态"> 
               <template slot-scope="scope">
                <el-tag v-if="scope.row.status === 0">草稿</el-tag>
                <el-tag v-else-if="scope.row.status === 1" type="info">待审核</el-tag>
                <el-tag v-else-if="scope.row.status === 2" type="success">审核通过</el-tag>
                <el-tag v-else-if="scope.row.status === 3" type="warning">审核失败</el-tag>
                <el-tag v-else-if="scope.row.status === 4" type="danger">已删除</el-tag>
            </template>
        </el-table-column>
        <el-table-column align="center" prop="pubdate" label="发布时间"> 
        </el-table-column>
        <el-table-column align="center" prop="address" label="操作"> 
            <template slot-scope="scope">
                 <el-button size="mini" type="primary" icon="el-icon-edit" circle @click="editArticle(scope.row.id)"></el-button>
                 <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="deleteArticleList(scope.row.id)"></el-button>
            </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pagination"
        background
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :total="totalCount">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticlesList , getChannelsList,deleteArticle } from '@/api/article/article.js'
import { comfirm } from '@/utils/comfirm.js'
export default {
  name: "AtricleIndex",
  data() {
    return {
      queryInfo: {
        status:null,
        channel_id:null,
        begin_pubdate: null,
        end_pubdate: null,
        page: 1,
        per_page:10,
      },
      totalCount: 0,
      value1: "",
      tableData: [],
      channels: [],
      isLoading:false
    };
  },

  mounted() {
      this.getArticles()
      this.getChannels()
  },

  methods: {
    onSubmit() {
      console.log("submit!");
    },
    async getArticles (){
        this.isLoading = true
        const articles = await getArticlesList(this.queryInfo)
        this.totalCount = articles.data.total_count
        this.tableData = articles.data.results
        this.isLoading = false
        // console.log(articles);
    },
    // 改变页码，获取数据
    handleCurrentChange (currentPage){
        // console.log(currentPage);
        this.queryInfo.page = currentPage
        this.getArticles(this.queryInfo)
    },
    // 获取频道
   async getChannels (){
      const channels = await getChannelsList()
      this.channels = channels.data.channels 
      console.log(channels);
    },
    // 获取时间
    changeDate (time){
        if(time !== null){
            this.value1 = time
            this.queryInfo.begin_pubdate= this.value1[0].replace('”','').replace('“','')
            this.queryInfo.end_pubdate = this.value1[1].replace('“','').replace('”','')
        }else{
            this.queryInfo.begin_pubdate= null
            this.queryInfo.end_pubdate = null
        }
        
    },
    // 筛选文章
    searchArticle (){
        this.getArticles()
    },
    // 清空选择框
    clearSelect (){
        this.queryInfo.channel_id = null
    },
    // 删除文章
    async deleteArticleList (id){
        try {
           const confir =  await comfirm('文章')
           if(confir === "confirm"){
           const deleteRes =  await deleteArticle(id.toString())
             if(deleteRes.status === 204){
                this.$message.success('删除成功')
                this.getArticles()
            }
           }
        } catch (error) {
            this.$message.info('删除失败')
        }
    },
    // 编辑文章
    editArticle (id){
      this.$router.push({path:'/publish',query:{id:id.toString()}})
    }
  },
};
</script>

<style lang="less" scoped>
.box-card {
  margin-bottom: 20px;
}
.pagination{
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
}
</style>